package cn.baiweigang.uu.api.linearswap;

import cn.baiweigang.uu.BTContext;
import cn.baiweigang.uu.api.BaseApi;
import cn.baiweigang.uu.constants.BtcConfig;
import cn.baiweigang.uu.enums.ContractCodeEnum;
import cn.baiweigang.uu.enums.PeriodEnum;
import cn.baiweigang.uu.model.KLineInfo;
import cn.baiweigang.uu.msg.MsgPublisher;
import cn.baiweigang.uu.msg.PriceChangeEvent;
import cn.baiweigang.uu.utils.DateUtil;
import cn.baiweigang.uu.utils.LogUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.huobi.linearswap.wss.event.MarketKLineSubResponse;
import com.huobi.linearswap.wss.handle.WssMarketHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
public class MarketWs extends BaseApi {
    private static String URL = "wss://" + BtcConfig.HB_HOST + "/linear-swap-ws";//合约站行情请求以及订阅地址

    private WssMarketHandle wssMarketHandle;
    private long preTs = 0;
    private BigDecimal prePrice = new BigDecimal("0");

    @Autowired
    private MsgPublisher msgPublisher;

    public boolean sub(ContractCodeEnum contractCode, PeriodEnum period) {
        wssMarketHandle = new WssMarketHandle(URL);
        List<String> channels = Lists.newArrayList();
        channels.add("market." + contractCode.getCode() + ".kline." + period.getCode());
        try {
            wssMarketHandle.sub(channels, response -> {
//                LogUtil.logDebug(response);
                long currentTimeMillis = DateUtil.getTs();
                BTContext.OrderPriceRefreshTs = currentTimeMillis;
                MarketKLineSubResponse event = JSON.parseObject(response, MarketKLineSubResponse.class);
                MarketKLineSubResponse.TickBean tickBean = event.getTick();
                //报价差大于50 或者超过30秒  发出消息
                if (currentTimeMillis - preTs > 10 * 1000 ||
                        tickBean.getClose().subtract(prePrice).intValue() > 50) {
                    preTs = currentTimeMillis;
                    prePrice = event.getTick().getClose();
                    KLineInfo kLineInfo = new KLineInfo();
                    kLineInfo.setVol(tickBean.getVol());
                    kLineInfo.setOpen(tickBean.getOpen());
                    kLineInfo.setLow(tickBean.getLow());
                    kLineInfo.setHigh(tickBean.getHigh());
                    kLineInfo.setCount(tickBean.getCount());
                    kLineInfo.setClose(tickBean.getClose());
                    kLineInfo.setAmount(tickBean.getAmount());
                    kLineInfo.setTradeTurnover(tickBean.getTradeTurnover());
                    kLineInfo.setId(tickBean.getId());
                    kLineInfo.setDateTime(DateUtil.formatDateYMDHMS(new Date(kLineInfo.getId() * 1000)));
                    PriceChangeEvent priceChangeEvent = new PriceChangeEvent("ws",
                            contractCode, period, kLineInfo);
//                    LogUtil.logInfo("发出消息");
//                    LogUtil.logInfo(priceChangeEvent);
                    msgPublisher.publish(priceChangeEvent);
                }
            });
            return true;
        } catch (Throwable e) {
            LogUtil.logError(e);
        }
        return false;
    }

    public void setWsNull() {
        if (wssMarketHandle != null) wssMarketHandle.close();
        wssMarketHandle = null;
    }
}
